package com.spark.incoicings.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.spark.incoicings.dao.OrderDetailMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.dao.ReturnDetailMapper;
import com.spark.incoicings.dao.SalesOrderMapper;
import com.spark.incoicings.service.DictTypeService;
import com.spark.incoicings.service.SalesReturnService;
import com.spark.incoicings.vo.DictVO;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Slf4j
public class SalesReturnController {
    @Resource
    private SalesReturnService returnService;
    @Resource
    private DictTypeService dictTypeService;
    @Resource
    private ReturnDetailMapper returnDetailMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private OrderDetailMapper detailMapper;
    @Resource
    private SalesOrderMapper salesOrderMapper;
    /**
     * 查询退货状态
     * @return
     */
    @GetMapping("/selectEntryStatus")
    public Result selectEntryStatus(){
        List<DictVO> shipment = dictTypeService.selectShipment("entry_status");
        return Result.success(200,"success",shipment);
    }
    /**
     * 查询退货原因
     * @return
     */
    @GetMapping("/selectReasonReturn")
    public Result selectReasonReturn(){
        List<DictVO> shipment = dictTypeService.selectShipment("reason_return");
        return Result.success(200,"success",shipment);
    }

    @GetMapping("/selectSalesReturnAll")
    public Result selectSalesReturnAll(@RequestParam(defaultValue = "1")Integer pageNum,
                                       @RequestParam(defaultValue = "3")Integer pageSize,
                                       @RequestParam(required = false)String customerName,
                                       @RequestParam(required = false)Integer id,
                                       @RequestParam(required = false)Integer returnStatusId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("customerName",customerName);
        map.put("returnStatusId",returnStatusId);
        PageHelper.startPage(pageNum,pageSize);
        List<SalesReturn> salesReturns = returnService.selectSalesReturnAll(map);
        PageInfo<SalesReturn> pageInfo = new PageInfo<>(salesReturns);
        return Result.success(200,"success",pageInfo);
    }
    @PostMapping("/updateSalesReturn")
    public Result updateSalesReturn(@RequestBody SalesReturn salesReturn){
        returnService.updateById(salesReturn);
        return Result.success(200,"success",salesReturn);
    }
    @PostMapping("/saveSalesReturn")
    @Transactional
    public Result saveSalesReturn(@RequestBody SalesReturn salesReturn){
        log.info("salesReturn={}",salesReturn);
        returnService.save(salesReturn);
        Integer num = 0;//退货总数
        double  numPrice = 0.0;//退货总额
        for (ReturnDetail returnDetail : salesReturn.getReturnDetails()) {
            returnDetail.setRefundAmount(returnDetail.getReturnQuantity()
                    *returnDetail.getReturnPrice());
            returnDetail.setReturnOrderId(salesReturn.getReturnId());
            num+= returnDetail.getReturnQuantity();
            numPrice+=returnDetail.getRefundAmount();
            returnDetailMapper.insert(returnDetail);
        }
        salesReturn.setTotalRefundAmount(numPrice);
        salesReturn.setTotalReturnQuantity(num);
        salesReturn.setNegativeCommission(numPrice*(3.0/100));
        returnService.updateById(salesReturn);
        return Result.success(200,"success",salesReturn);
    }

    /**
     * 修改退货单及退货金额
     * @param salesReturn
     * @return
     */
    @PostMapping("/updateSalesReturnTow")
    @Transactional
    public Result updateSalesReturnTow(@RequestBody SalesReturn salesReturn){
        log.info("salesReturn={}",salesReturn);
        Integer num = 0;//退货总数
        double  numPrice = 0.0;//退货总额
        for (ReturnDetail returnDetail : salesReturn.getReturnDetails()) {
            returnDetail.setRefundAmount(returnDetail.getReturnQuantity()
                    *returnDetail.getReturnPrice());
            returnDetail.setReturnOrderId(salesReturn.getReturnId());
            num+= returnDetail.getReturnQuantity();
            numPrice+=returnDetail.getRefundAmount();
            returnDetailMapper.updateById(returnDetail);
        }
        salesReturn.setTotalRefundAmount(numPrice);
        salesReturn.setTotalReturnQuantity(num);
        salesReturn.setNegativeCommission(numPrice*(3.0/100));
        returnService.updateById(salesReturn);
        return Result.success(200,"success",salesReturn);
    }
    @GetMapping("/selectSalesReturnById")
    public Result selectSalesReturnById(Integer id){
        SalesReturn salesReturn = returnService.getById(id);
        List<ReturnDetail> returnDetails = returnDetailMapper.selectList(new QueryWrapper<ReturnDetail>()
                .eq("return_order_id", salesReturn.getReturnId()));
        returnDetails.forEach(returnDetail -> {
            Product product = productMapper.selectById(returnDetail.getProductId());
            OrderDetail orderDetail = detailMapper.selectById(returnDetail.getOrderDetailId());
            SalesOrder salesOrder = salesOrderMapper.selectById(orderDetail.getSalesOrderId());
            orderDetail.setSalesOrder(salesOrder);
            returnDetail.setProduct(product);
            returnDetail.setOrderDetail(orderDetail);
        });
        salesReturn.setReturnDetails(returnDetails);
        return Result.success(200,"success",salesReturn);
    }
}
